package px.Exam.Exam01;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class ServiceTest1 {
    public static void main(String[] args){
        System.out.println("服务端启动");
        try (ServerSocket serverSocket = new ServerSocket(7788);
             Socket socket = serverSocket.accept();// 连接对象
             //获取客户端信息
             InputStream in = socket.getInputStream();
             DataInputStream input = new DataInputStream(in);

             //发送服务端谢谢
             OutputStream out = socket.getOutputStream();  // 获取连接的输出流，写东西到服务端、
             DataOutputStream dataOut = new DataOutputStream(out);
             Scanner sc = new Scanner(System.in)){ // 获取与客户端建立连接的对象)
            System.out.println("客户端连接成功");
            while (true) {
                //接受客户端信息
                String message = input.readUTF();
                System.out.println("接收到用户：" + socket.getInetAddress() + "，端口：" + socket.getPort() + "的消息：" + message);

                //服务端回复
                String src = sc.nextLine();
                dataOut.writeUTF(src);
                dataOut.flush();
                }
            }catch (IOException e){
            e.getMessage();
        }
    }
}

